brute force constructive algorithms strings *1200

Please click on ads to support us..

Python Code:

def intToChar(x):     return chr(ord('a')+x)

allsubstrings = []
for mask in range(26):
    arr = [mask]
    ss = ''.join(intToChar(x) for x in arr)
    allsubstrings.append(ss)
for mask in range(26 ** 2):
    arr = []
    for _ in range(2):
        arr.append(mask % 26)
        mask //= 26
    arr.reverse()
    ss = ''.join(intToChar(x) for x in arr)
    allsubstrings.append(ss)
for mask in range(26 ** 3):
    arr = []
    for _ in range(3):
        arr.append(mask % 26)
        mask //= 26
    arr.reverse()
    ss = ''.join(intToChar(x) for x in arr)
    allsubstrings.append(ss)

allsubstrings = allsubstrings[: 3000]

def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        n = int(input())
        s = input()
        substrings = set()
        for i in range(n):
            substrings.add(s[i])
            if i + 1 < n:
                substrings.add(s[i: i + 2])
            if i + 2 < n:
                substrings.add(s[i: i + 3])
        for ss in allsubstrings:
            if ss not in substrings:
                ans = ss
                break
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return

import sys
input=lambda: sys.stdin.readline().rstrip("\r\n")  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define ll long long
#define f(i,n) for(int i=0;i<n;i++)
#define fi(i,x,n) for(int i=x;i<n;i++)
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define all(x)            (x).begin(),(x).end()
#define rof(i,a,b) for(int i=(a);i>=(b);i--)
#define S second
#define F first
#define is insert
#define sz(a) (int)(a.size())

void solve() {

    int n; cin >> n;
    string s;
    cin >> s;
    bool ans = false;
    int count[26] = {0};
    for (int i = 0; i < n; i++) {
        count[s[i] - 'a'] = 1;
    }

    for (int i = 0; i < 26; i++) {
        if (count[i] == 0) {
            cout << (char)('a' + i) << '\n';
            return;
        }
    }
    {
        set<string>st;
        for (int i = 0; i < n - 1; i++) {
            string temp = "";
            temp += s[i];
            temp += s[i + 1];
            st.insert(temp);
        }


        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                string temp = "";
                temp += (char)('a' + i);
                temp += (char)('a' + j);
                if (st.find(temp) == st.end()) {
                    cout << temp << "\n";
                    return;
                }
            }
        }
    }
    {
        set<string>st;
        for (int i = 0; i < n - 2; i++) {
            string temp = "";
            temp += s[i];
            temp += s[i + 1];
            temp += s[i + 2];
            st.insert(temp);
        }


        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                string temp = "a";
                temp += (char)('a' + i);
                temp += (char)('a' + j);
                if (st.find(temp) == st.end()) {
                    cout << temp << "\n";
                    return;
                }
            }
        }
    }

}

int32_t main() {
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        //cout << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game